package com.prp.gwt.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedTabBar;
import com.google.gwt.user.client.ui.LazyPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.user.article.ArticleHistoryHandler;
import com.prp.gwt.client.user.friend.FriendHistoryHandler;
import com.prp.gwt.client.user.home.HomeHistoryHandler;
import com.prp.gwt.client.user.mail.MailHistoryHandler;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.user.profile.ProfileHistoryHandler;
import com.prp.gwt.client.widgets.mail.MailView;
import com.prp.gwt.client.widgets.user.article.ArticleView;
import com.prp.gwt.client.widgets.user.friend.FriendView;
import com.prp.gwt.client.widgets.user.home.HomeView;
import com.prp.gwt.client.widgets.user.php.PHPView;
import com.prp.gwt.client.widgets.user.profile.ProfileView;

public class UserBarPanel extends VerticalPanel {
	  
	private static UserBarPanel instance;
	private static Dictionary dictionary = Dictionary
			.getDictionary(ClientConstants.JS_CONSTANTS);
	private DecoratedTabBar bar = new DecoratedTabBar();
	private DeckPanel deck = new DeckPanel();
	
	public static UserBarPanel getInstance() {
		if (instance == null)
			instance = new UserBarPanel();
		return instance;
	}

	private UserBarPanel() {
		addItem(ClientConstants.MODULE_HOME, HomeView.getInstance(), new HomeHistoryHandler(), HomeHistoryHandler.INIT_VALUE);
		addItem(ClientConstants.MODULE_PHP, PHPView.getInstance(), new PHPHistoryHandler(), PHPHistoryHandler.INIT_VALUE);
		addItem(ClientConstants.MODULE_FRIEND, FriendView.getInstance(), new FriendHistoryHandler(), FriendHistoryHandler.INIT_VALUE);
		addItem(ClientConstants.MODULE_PROFILE, ProfileView.getInstance(), new ProfileHistoryHandler(), ProfileHistoryHandler.INIT_VALUE);
		addItem(ClientConstants.MODULE_ARTICLE, ArticleView.getInstance(), new ArticleHistoryHandler(), ArticleHistoryHandler.INIT_VALUE);
		addItem(ClientConstants.MODULE_MAIL, MailView.getInstance(), new MailHistoryHandler(), MailHistoryHandler.INIT_VALUE);
		add(bar);
		bar.selectTab(0);
		RootPanel.get("content").add(deck);
	}

	private void addItem(String name, final Widget widget, ValueChangeHandler<String> handler, final String initValue){
		bar.addTab(dictionary.get(name));
		final LazyPanel lazyPanel = new LazyPanel(){
			@Override
			protected Widget createWidget() {
				return widget;
		}};
		deck.add(lazyPanel);
		if(handler != null)
			History.addValueChangeHandler(handler);
		bar.getTab(bar.getTabCount()-1).addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(initValue);
			}
		});
	}
	
	public void selectTab(int index){
		if(index < bar.getTabCount()){
			bar.selectTab(index);
			((LazyPanel)deck.getWidget(index)).ensureWidget();
			deck.showWidget(index);
		}
	}
	
}
